/** <!--------------------------------------------------------------------------------------------->
 * @file GraphWindow.cpp
 *
 * GSvn - A Graphical Client for SVN
 * Copyright (C) 2012  Karl Krach, gsvn@bluespirit.la
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 <!---------------------------------------------------------------------------------------------> */


// -- includes --
#include "GraphWindow.h"
#include "ui_GraphWindow.h"
#include <QFileSystemModel>
#include <QDebug>
#include "GraphRevision.h"
#include "PlaceholderItem.h"
#include <QDesktopWidget>
#include "GraphBranch.h"
#include "GraphBranchFrom.h"
#include "GraphBranchTo.h"

/** <!--------------------------------------------------------------------------------------------->
 * Creates the GraphWindow object
 *
 * @param pParent pointer to parent object
 <!---------------------------------------------------------------------------------------------> */
GraphWindow::GraphWindow( QWidget* pParent ) : QMainWindow(pParent), ui(new Ui::GraphWindow)
{
	ui->setupUi(this);

	// Center window on the screen
	setGeometry( QStyle::alignedRect( Qt::LeftToRight, Qt::AlignCenter, size(),
	                                  qApp->desktop()->availableGeometry() ) );

	//
	// Initializes the tree view
	//
	QFileSystemModel* pFileSystemModel = new QFileSystemModel(this);
	pFileSystemModel->setRootPath( QDir::currentPath() );
	ui->m_pTreeView->setModel(pFileSystemModel);
	ui->m_pTreeView->setRootIndex( pFileSystemModel->index(QDir::currentPath()) );
	ui->m_pTreeView->setColumnHidden( 1, true );
	ui->m_pTreeView->setColumnHidden( 2, true );
	ui->m_pTreeView->setColumnHidden( 3, true );
	ui->m_pTreeView->setColumnHidden( 4, true );


	//
	// Hide the tool bar
	//
	ui->m_pToolbar->hide();
	ui->m_pStatusbar->hide();

	//
	// Hides the tree view
	//
	ui->m_pSplitter->setSizes( QList<int>() << 0 << 100 );

	m_oScene.setSceneRect( QRect(-5, -5, 10, 10) );
	ui->m_pGraphicsView->setScene( &m_oScene );


	//
	// Adding Branch
	//
	{
		GraphBranch* pBranchB = new GraphBranch( "Branch B" );
		m_oScene.addItem( pBranchB );
		GraphBranch* pBranchA = new GraphBranch( "Branch A" );
		m_oScene.addItem( pBranchA );
		GraphBranch* pTrunk = new GraphBranch( "Trunk" );
		m_oScene.addItem( pTrunk );

		AddRevision( pTrunk, pBranchA, pBranchB, "r1" );
		AddRevision( pTrunk, pBranchA, pBranchB, "r2" );
		AddBranch( pTrunk, pBranchB, pBranchA );
		AddRevision( pBranchB, pBranchA, pTrunk, "+r3" );
		AddRevision( pBranchB, pBranchA, pTrunk, "r4" );
		AddBranch( pTrunk, pBranchA, pBranchB );
		AddRevision( pTrunk, pBranchB, pBranchA, "f5" );
		AddRevision( pBranchA, pBranchB, pTrunk, "+r6" );
		AddRevision( pTrunk, pBranchA, pBranchB, "r7" );
		AddRevision( pBranchB, pBranchA, pTrunk, "r8" );
		AddRevision( pTrunk, pBranchA, pBranchB, "r9" );
	}
}	// END GraphWindow( QWidget* )

void GraphWindow::AddRevision( GraphBranch* pBranch, GraphBranch* pEmptyBranch1,
                               GraphBranch* pEmptyBranch2,QString strText )
{
	GraphItem* pItem = new GraphRevision( pBranch, strText );
	new PlaceholderItem( pEmptyBranch1, pItem );
	new PlaceholderItem( pEmptyBranch2, pItem );
}

void GraphWindow::AddBranch(  GraphBranch* pFrom, GraphBranch* pTo,
                             GraphBranch* pEmptyBranch )
{
	new GraphBranchFrom( pFrom, pTo );		// trunk
	GraphItem* pToItem   = new GraphBranchTo( pTo, pFrom );	// branch
	new PlaceholderItem( pEmptyBranch, pToItem );
}

/** <!--------------------------------------------------------------------------------------------->
 * Deletes the GraphWindow object
 <!---------------------------------------------------------------------------------------------> */
GraphWindow::~GraphWindow()
{
	delete ui;

}	// END ~GraphWindow()

/** <!----------------------------------------------------------------------------------------->
 * Callback method to be called on each history element
 *
 * @param iRevision  (in) SVN revision
 * @param strMessage (in) SVN log message
 * @param strAuthor  (in) SVN author
 <!-----------------------------------------------------------------------------------------> */
void GraphWindow::OnHistoryReceive( int iRevision, QString strMessage, QString strAuthor )
{
	qDebug() << QString( "[%1] %2 of %3" ).arg(iRevision).arg(strMessage).arg(strAuthor);

}	// END OnHistoryReceive( int, QString, QString )

/** <!----------------------------------------------------------------------------------------->
 * Callback method to be called on every progress-update
 *
 * @param fBytesReceived (in) Received bytes
 * @param fThrougput     (in) Throughput in kB/sec
 <!-----------------------------------------------------------------------------------------> */
void GraphWindow::OnHistoryProgress( float fBytesReceived, float fThrouput )
{
	qDebug() << fBytesReceived << "bytes " << fThrouput << "kB/s";

}	// END OnHistoryProgress( float, float )

/** <!----------------------------------------------------------------------------------------->
 * Callback method to be called on every statistic update
 *
 * @param strCommand (in) Name of last command
 * @param iMsNeeded  (in) Milliseconds need for the last command
 <!-----------------------------------------------------------------------------------------> */
void GraphWindow::OnHistoryStatistics( QString strCommand, int iMsNeeded )
{
	qDebug() << strCommand << " finished in " << iMsNeeded << "ms";

}	// END OnHistoryStatistics( QString, int )
